$(function() {
    /**
    * 支付弹出层
    */
    (function(){
        $('#j-modal-close').on('click', function() {
        	$('#j-modal').fadeOut(500);
            $('#j-mask').fadeOut(500);
        });

        disabled = false;
        $('#j-btn-pay').on('click', function() {
        	if(disabled){
        		var d = dialog({
			        "title": "家家户户网提示您",
			        "width":"220",
			        "content":'数据正在提交中',
			        "okValue":"确定",
			        "ok": function(){}
			        }).showModal();
        	}
        	pay_type = $("#j-payment").val();
        	disabled = true;
        	if(pay_type=='alipay') var newTab = window.open('about:blank');
        	$.ajax({
    	        url:url,
    	        type:"post",
    	        data:"pay_type="+pay_type+"&id="+id,
    	        beforeSend:function(){
    	        	$('#j-mask').fadeIn(500);
    	        },
    	        success:function(data){
    	        	if (0 == data.status) {
    	        		var d = dialog({
    				        "title": "家家户户网提示您",
    				        "width":"220",
    				        "content":data.info,
    				        "okValue":"确定",
    				        "ok": function(){}
    				        }).showModal();
    	            }else{
    	            	$('#j-modal').fadeIn(500);
    	            	switch(data.data.pay_type){
    	            		case 'wechatpay':
    	            			$('#j-modal').find('div.icon>img').attr('src', data.data.src);
    	            			break;
    	            		case 'alipay':
    	            		    newTab.location.href = data.data.url;
    	            			break;
    	            	}
    	            }
    	        	disabled = false;
    	        },
    	        error:function (XMLHttpRequest, textStatus, errorThrown) {
    	          var d = dialog({
				        "title": "家家户户网提示您",
				        "width":"220",
				        "content":"请求错误："+textStatus,
				        "okValue":"确定",
				        "ok": function(){}
				        }).showModal();
    	          
    	          disabled = false;
    	        }
    	    });
        	
        });
    })();

    /**
    * 自定义checkbox
    */
    (function(){
        $('div.u-checkbox').on('click', function(){
            var payment_id = $(this).data('id');
            $('#j-payment').val(payment_id);
            $('div.u-checkbox').removeClass('u-checkbox-active');
            $(this).addClass('u-checkbox-active');
            if (payment_id == 'alipay'){
                $('#j-modal').find('.info>h1').html('请您在打开的页面上完成付款');
                $('#j-modal').find('div.icon').removeAttr('icon_code');
            	$('#j-modal').find('div.icon>img').attr('src', img_url+'/tips.jpg');
            } else {
                $('#j-modal').find('.info>h1').html('请使用微信扫面右侧二维码');
                $('#j-modal').find('div.icon>img').removeAttr('src');
                $('#j-modal').find('div.icon').addClass('icon_code');
            }
        });
    })();
});

